// src/core/service-types.ts

import type { Store, StoreDefinition } from 'pinia';

/**
 * 服务基础接口
 */
export interface BaseService {
  init?(ctx: ModuleContext): Promise<void> | void;
  destroy?(): void;
}

/**
 * 状态存储接口
 */
export interface ModuleStateStore extends Store {
  // 重置状态（保留初始值）
  $resetToInitial(): void;
  
  // 模块ID
  readonly moduleId: ModuleId;
}

/**
 * 服务工厂方法
 */
export type ServiceFactory<T extends BaseService> = 
  (ctx: ModuleContext) => T | Promise<T>;

/**
 * 状态存储工厂方法
 */
export type StateFactory<T extends Store> = 
  (id: string, options?: any) => StoreDefinition<string, any, any, any>;

/**
 * 服务注册器
 */
export class ServiceRegistry {
  private readonly services = new Map<ServiceKey<any>, BaseService>();
  
  register<T extends BaseService>(key: ServiceKey<T>, service: T): void {
    if (this.services.has(key)) {
      throw new Error(`服务 '${key}' 已注册`);
    }
    this.services.set(key, service);
  }
  
  get<T extends BaseService>(key: ServiceKey<T>): T | undefined {
    return this.services.get(key) as T | undefined;
  }
}